package forum;
import java.util.ArrayList;

public class middleEnd {

	db_code db = new db_code();
	
	
	
	public String getPosts(String input){
		int threadID = Integer.parseInt(input);
		
		ArrayList<Post> posts = db.getAllPostsInAThread(threadID);
		
		StringBuilder res = new StringBuilder();
		for(Post p :posts){
			res.append("<div>");
				res.append(p.toString());
			res.append("</div>");
		}
		return res.toString();
		
	}
	
	public String getThreads(String input){
		
		int subForumID = Integer.parseInt(input);
		ArrayList<Thread> threads = db.getAllThreadsBySubForum(subForumID);
	
		Thread thread;
		StringBuilder res = new StringBuilder();
		
		res.append("<table><tr>");
			res.append("<td>Name</td>");
			res.append("<td>CreatedBy</td>");
			res.append("<td>CreatedOn</td>");
			res.append("<td>Replies</td>");
			res.append("<td>Views</td>");
		res.append("</tr>");
		for(int i = 0; i < threads.size(); i++){
			thread = threads.get(i);
			
			res.append("<div id=\"subforum\">");
				res.append("<pre><tr>");
				res.append("<td><a href=\"thread.jsp?threadID="+thread.getThreadID()+"\">"+ thread.getName() + "</td></a>");
				res.append("<td>" + db.getUserName(thread.getCreatedBy()) + "</td>");
				res.append("<td>" + thread.getCreatedOn() + "</td>"); 
				res.append("<td>" + thread.getReplies() + "</td>");
				res.append("<td>" + thread.getViews() + "</td>");
				res.append("</tr></pre>");
			res.append("</div>");
			
		}
		res.append("</table>");
		return res.toString();
		
	}
	
	
	
	public String getSubforums(){
		ArrayList<SubForum> subforums = db.getAllSubForums();
		SubForum s;
		StringBuilder res = new StringBuilder();
		for(int i = 0; i < subforums.size(); i++){
			s = subforums.get(i);
			res.append("<div id=\"subforum\"><a href=\"subforum.jsp?subForumID="+s.getSubForumID()+"\">");
			res.append("<b>Name: </b> 				" + s.getName());
			res.append("<b>Number Of Threads:</b>	" + s.getNumberOfThreads()); 
			res.append("<b>Created On:</b> 			" + s.getCreatedOn());
			res.append("<b>Level:</b> 				" + s.getMinimumlvl());
				
			res.append("</a></div>");
		}
		
		return res.toString();
	}
}
